package com.baidu.automata.core.context;

import java.util.HashMap;
import java.util.Map;

import com.baidu.automata.core.type.DataField;

public class Context implements IContext {
	private String id = null;

	private Context parent = null;

	private Map<String, DataField> fields = new HashMap<String, DataField>();

	public Context(String id) {
		super();
		this.id = id;
	}

	@Override
	public String getId() {
		return id;
	}

	public Context getParent() {
		return parent;
	}

	public void setParent(Context parent) {
		this.parent = parent;
	}

	@Override
	public void addDataField(DataField f) {
		this.fields.put(f.getId(), f);
	}

	@Override
	public DataField getDataFieldById(String id) {
		return (this.fields.get(id) == null && this.parent != null) ? this.parent.getDataFieldById(id) : this.fields.get(id);
	}

	@Override
	public DataField removeDataFieldById(String id) {
		return (this.fields.get(id) == null && this.parent != null) ? this.parent.removeDataFieldById(id) : this.fields.remove(id);
	}
}
